<?php
/**
 * Created by PhpStorm.
 * User: 35083
 * Date: 2019/7/16
 * Time: 16:36
 */

namespace app\task\controller;


use app\admin\model\Sendwxmsg;

class Sleepreport
{

    /**
     * @param $key
     * @param $param
     * @return int
     */
    public function task($key, $param)
    {

        $userInfo = array('id' => $param['uid']);

        $dataid = $param['dataid'];

        $apiLog = (new \app\admin\model\SleepApiLog())->getDataForWhere(['dataid' => $dataid, 'ty' => $param['ty']]);
        if ($apiLog['api_get_count'] > $apiLog['max_count']) {//超过255就不在抓取了
            return 2;
        }

        //增加1次
        (new \app\admin\model\SleepApiLog())->updateDataForWhere(['api_get_count' => $apiLog['api_get_count'] + 1], ['dataid' => $dataid, 'ty' => $param['ty']]);

        $result = recordFiles($userInfo, $dataid, $apiLog['ty']);
        (new \app\admin\model\Debug())->add([
                'txt' => $result,
                'ty' => 1,
                'txt1' => json_encode($param, true),
                'ctime' => date("Y-m-d H:i:s")
            ]
        );
        $result = json_decode($result, true);

        if ($result['result'] == "ok") {

            $lists = $result['list'];
            if (!empty($lists)) {
                $data = $lists[0];
                if ($data['status'] == "1")//0分析失败 1分析成功
                {
                    addSleepReportSummaryQueue($param);
                    //报告成功
                    return 0;
                } else if ($data['status'] == "0") {
                    //报告报还在生成中 需要再次获取
                    addSleepReportQueue($param);
                    return 999;
                } else {
                    //分析失败
                    return 1000;
                }
            }
            return 1001;
        } else {
            return 1002;
        }

    }


    public function taskpre($key, $param)
    {

        $userInfo = array('id' => $param['uid']);

        $testid = $param['testid'];
        $device = $param['device'];
        $ty = $param['ty'];

        $apiLog = (new \app\admin\model\SleepApiLogPre())->getDataForWhere(['testid' => $testid, 'device' => $device, 'ty' => $ty, 'is_close' => 0]);
        if ($apiLog['api_get_count'] > $apiLog['max_count']) {//超过255就不在抓取了
            return 2;
        }
        if (empty($apiLog)) {
            return 3;
        }
        //增加1次
        (new \app\admin\model\SleepApiLogPre())->updateDataForWhere(['api_get_count' => $apiLog['api_get_count'] + 1], ['id' => $apiLog['id']]);

        $result = getFilesSilanStatus($userInfo, $device, $testid, $apiLog['ty']);
        (new \app\admin\model\Debug())->add([
                'txt' => $result,
                'ty' => 1,
                'ctime' => date("Y-m-d H:i:s")
            ]
        );
        $result = json_decode($result, true);

        if ($result['status'] == "1") {
            $dataid = $result['dataid'];
            addSleepApiLog($userInfo['id'], $dataid, $apiLog['ty'], $apiLog['testid'], $apiLog['device']);
            //向队列中增加一个获取报告的任务
            $param = array('uid' => $userInfo['id'], 'dataid' => $dataid, 'ty' => $apiLog['ty']);
            addSleepReportQueue($param);

            (new \app\admin\model\SleepApiLogPre())->updateDataForWhere(['is_close' => 1], ['id' => $apiLog['id']]);

            return 0;
        } else {
            addSleepReportQueuePre($param);
            return 1002;
        }
    }


    public function taskpre10($key, $param)
    {

        $userInfo = array('id' => $param['uid']);

        $testid = $param['testid'];
        $device = $param['device'];
        $ty = $param['ty'];

        $apiLog = (new \app\admin\model\SleepApiLogPre())->getDataForWhere(['testid' => $testid, 'device' => $device, 'ty' => $ty, 'is_close' => 0]);
        if ($apiLog['api_get_count'] > $apiLog['max_count']) {//超过255就不在抓取了
            return 2;
        }
        if (empty($apiLog)) {
            return 3;
        }
        //增加1次
        (new \app\admin\model\SleepApiLogPre())->updateDataForWhere(['api_get_count' => $apiLog['api_get_count'] + 1], ['id' => $apiLog['id']]);

        $result = getFilesSilanStatus($userInfo, $device, $testid, $apiLog['ty']);
        (new \app\admin\model\Debug())->add([
                'txt' => $result,
                'ty' => 1,
                'ctime' => date("Y-m-d H:i:s")
            ]
        );
        $result = json_decode($result, true);

        if ($result['status'] == "1") {
            $dataid = $result['dataid'];
            addSleepApiLog($userInfo['id'], $dataid, $apiLog['ty'], $apiLog['testid'], $apiLog['device']);
            //向队列中增加一个获取报告的任务
            $param = array('uid' => $userInfo['id'], 'dataid' => $dataid, 'ty' => $apiLog['ty']);
            addSleepReportQueue($param);
            (new \app\admin\model\SleepApiLogPre())->updateDataForWhere(['is_close' => 1], ['id' => $apiLog['id']]);

            return 0;
        } else {
            addSleepReportQueuePre10($param);
            return 1002;
        }
    }


    public function taskloadsummary($key, $param)
    {
        $userInfo = array('id' => $param['uid']);
        $repid = $param['dataid'];
        $data = loadSummary($userInfo, $repid, $param['ty']);
        (new \app\admin\model\Debug())->add([
                'txt' => $data,
                'txt1' => json_encode($param, true),
                'ty' => 21,
                'ctime' => date("Y-m-d H:i:s")
            ]
        );
        $apiLog = (new \app\admin\model\SleepApiLog())->getDataForWhere(['dataid' => $param['dataid'], "ty" => $param['ty']]);
        $result = analysisSummaryData($data, $userInfo['id'], $repid, $apiLog['ty']);

        if ($result['err'] == 0)//放入队列获取报表
        {
            addSleepReportCurvesQueue($param);
        }
        return $result['err'];
    }

    public function taskloadcurves($key, $param)
    {
        $userInfo = array('id' => $param['uid']);
        $repid = $param['dataid'];
        $data = loadCurves($userInfo, $repid, $param['ty']);
        (new \app\admin\model\Debug())->add([
                'txt' => $data,
                'ty' => 3,
                'ctime' => date("Y-m-d H:i:s")
            ]
        );
        $apiLog = (new \app\admin\model\SleepApiLog())->getDataForWhere(['dataid' => $param['dataid'], 'ty' => $param['ty']]);
        $result = analysisCurvesData($data, $userInfo['id'], $repid, $apiLog['ty']);
        return $result['err'];
    }
}